
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Replication GraphQL · RxDB - JavaScript Database</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="replication-websocket.html" />
    
    
    <link rel="prev" href="replication.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="/" target="_blank" class="custom-link">RxDB</a>
        </li>
    
        
        <li>
            <a href="https://twitter.com/intent/user?screen_name=rxdbjs" target="_blank" class="custom-link">Follow @twitter </a>
        </li>
    
        
        <li>
            <a href="https://discord.gg/tqt9ZttJfD" target="_blank" class="custom-link">Chat @discord </a>
        </li>
    
        
        <li>
            <a href="https://github.com/pubkey/rxdb" target="_blank" class="custom-link">Star @github </a>
        </li>
    
        
        <li>
            <a href="https://github.com/sponsors/pubkey" target="_blank" class="custom-link">Donate @github </a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="quickstart.html">
            
                <a href="quickstart.html">
            
                    
                    Quickstart
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="install.html">
            
                <a href="install.html">
            
                    
                    Install
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="install.html">
            
                <a href="install.html#npm">
            
                    
                    npm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="install.html">
            
                <a href="install.html#import">
            
                    
                    import
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="dev-mode.html">
            
                <a href="dev-mode.html">
            
                    
                    Dev Mode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="rx-database.html">
            
                <a href="rx-database.html">
            
                    
                    RxDatabase
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="rx-database.html">
            
                <a href="rx-database.html#creation">
            
                    
                    Creation
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="rx-database.html">
            
                <a href="rx-database.html#name">
            
                    
                    name
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="rx-database.html">
            
                <a href="rx-database.html#adapter">
            
                    
                    adapter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.3" data-path="rx-database.html">
            
                <a href="rx-database.html#password">
            
                    
                    password
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.4" data-path="rx-database.html">
            
                <a href="rx-database.html#multiinstance">
            
                    
                    multiInstance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.5" data-path="rx-database.html">
            
                <a href="rx-database.html#eventreduce">
            
                    
                    eventReduce
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.6" data-path="rx-database.html">
            
                <a href="rx-database.html#ignoreduplicate">
            
                    
                    ignoreDuplicate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.7" data-path="rx-database.html">
            
                <a href="rx-database.html#pouchSettings">
            
                    
                    pouchSettings
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="rx-database.html">
            
                <a href="rx-database.html#functions">
            
                    
                    Functions
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="rx-database.html">
            
                <a href="rx-database.html#observe-with-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.2" data-path="rx-database.html">
            
                <a href="rx-database.html#dump">
            
                    
                    exportJSON()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.3" data-path="rx-database.html">
            
                <a href="rx-database.html#importdump">
            
                    
                    importJSON()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.4" data-path="rx-database.html">
            
                <a href="rx-database.html#server">
            
                    
                    server()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.5" data-path="rx-database.html">
            
                <a href="rx-database.html#waitforleadership">
            
                    
                    waitForLeadership()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.6" data-path="rx-database.html">
            
                <a href="rx-database.html#requestidlepromise">
            
                    
                    requestIdlePromise()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.7" data-path="rx-database.html">
            
                <a href="rx-database.html#destroy">
            
                    
                    destroy()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.8" data-path="rx-database.html">
            
                <a href="rx-database.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.9" data-path="rx-database.html">
            
                <a href="rx-database.html#checkadapter">
            
                    
                    checkAdapter()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.10" data-path="rx-database.html">
            
                <a href="rx-database.html#isrxdatabase">
            
                    
                    isRxDatabase()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="rx-schema.html">
            
                <a href="rx-schema.html">
            
                    
                    RxSchema
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="rx-schema.html">
            
                <a href="rx-schema.html#example">
            
                    
                    Example
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="rx-schema.html">
            
                <a href="rx-schema.html#create-a-collection-with-the-schema">
            
                    
                    Create a collection with the schema
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="rx-schema.html">
            
                <a href="rx-schema.html#version">
            
                    
                    version
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="rx-schema.html">
            
                <a href="rx-schema.html#keycompression">
            
                    
                    keyCompression
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="rx-schema.html">
            
                <a href="rx-schema.html#indexes">
            
                    
                    indexes
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="rx-schema.html">
            
                <a href="rx-schema.html#attachments">
            
                    
                    attachments
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="rx-schema.html">
            
                <a href="rx-schema.html#default">
            
                    
                    default
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="rx-schema.html">
            
                <a href="rx-schema.html#final">
            
                    
                    final
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="schema-validation.html">
            
                <a href="schema-validation.html">
            
                    
                    Schema Validation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="rx-collection.html">
            
                <a href="rx-collection.html">
            
                    
                    RxCollection
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="rx-collection.html">
            
                <a href="rx-collection.html#creating-a-collection">
            
                    
                    Creation
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1.1" data-path="rx-collection.html">
            
                <a href="rx-collection.html#name">
            
                    
                    name
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.1.2" data-path="rx-collection.html">
            
                <a href="rx-collection.html#schema">
            
                    
                    schema
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.1.3" data-path="rx-collection.html">
            
                <a href="rx-collection.html#orm-functions">
            
                    
                    ORM-functions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.1.4" data-path="rx-collection.html">
            
                <a href="rx-collection.html#Migration">
            
                    
                    Migration
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="rx-collection.html">
            
                <a href="rx-collection.html#functions">
            
                    
                    Functions
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.2.1" data-path="rx-collection.html">
            
                <a href="rx-collection.html#observe-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.2" data-path="rx-collection.html">
            
                <a href="rx-collection.html#insert">
            
                    
                    insert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.3" data-path="rx-collection.html">
            
                <a href="rx-collection.html#bulkinsert">
            
                    
                    bulkInsert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.4" data-path="rx-collection.html">
            
                <a href="rx-collection.html#bulkremove">
            
                    
                    bulkRemove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.5" data-path="rx-collection.html">
            
                <a href="rx-collection.html#upsert">
            
                    
                    upsert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.6" data-path="rx-collection.html">
            
                <a href="rx-collection.html#incrementalUpsert">
            
                    
                    incrementalUpsert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.7" data-path="rx-collection.html">
            
                <a href="rx-collection.html#find">
            
                    
                    find()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.8" data-path="rx-collection.html">
            
                <a href="rx-collection.html#findone">
            
                    
                    findOne()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.9" data-path="rx-collection.html">
            
                <a href="rx-collection.html#findbyids">
            
                    
                    findByIds()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.10" data-path="rx-collection.html">
            
                <a href="rx-collection.html#findbyids$">
            
                    
                    findByIds$()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.11" data-path="rx-collection.html">
            
                <a href="rx-collection.html#dump">
            
                    
                    exportJSON()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.12" data-path="rx-collection.html">
            
                <a href="rx-collection.html#importdump">
            
                    
                    importJSON()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.13" data-path="rx-collection.html">
            
                <a href="rx-collection.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.14" data-path="rx-collection.html">
            
                <a href="rx-collection.html#destroy">
            
                    
                    destroy()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2.15" data-path="rx-collection.html">
            
                <a href="rx-collection.html#isrxcollection">
            
                    
                    isRxCollection()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="rx-document.html">
            
                <a href="rx-document.html">
            
                    
                    RxDocument
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="rx-document.html">
            
                <a href="rx-document.html#insert">
            
                    
                    Insert
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="rx-document.html">
            
                <a href="rx-document.html#find">
            
                    
                    Find
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="rx-document.html">
            
                <a href="rx-document.html#functions">
            
                    
                    Functions
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.3.1" data-path="rx-document.html">
            
                <a href="rx-document.html#get">
            
                    
                    get()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.2" data-path="rx-document.html">
            
                <a href="rx-document.html#get$">
            
                    
                    get$()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.3" data-path="rx-document.html">
            
                <a href="rx-document.html#proxy-get">
            
                    
                    proxy-get
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.4" data-path="rx-document.html">
            
                <a href="rx-document.html#update">
            
                    
                    update()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.5" data-path="rx-document.html">
            
                <a href="rx-document.html#incrementalModify">
            
                    
                    incrementalModify()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.6" data-path="rx-document.html">
            
                <a href="rx-document.html#incrementalPatch">
            
                    
                    incrementalPatch()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.7" data-path="rx-document.html">
            
                <a href="rx-document.html#observe-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.8" data-path="rx-document.html">
            
                <a href="rx-document.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.9" data-path="rx-document.html">
            
                <a href="rx-document.html#deleted$">
            
                    
                    deleted$
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.10" data-path="rx-document.html">
            
                <a href="rx-document.html#tojson">
            
                    
                    toJSON()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.11" data-path="rx-document.html">
            
                <a href="rx-document.html#set">
            
                    
                    set()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.12" data-path="rx-document.html">
            
                <a href="rx-document.html#save">
            
                    
                    save()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3.13" data-path="rx-document.html">
            
                <a href="rx-document.html#isrxdocument">
            
                    
                    isRxDocument()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="rx-query.html">
            
                <a href="rx-query.html">
            
                    
                    RxQuery
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="rx-query.html">
            
                <a href="rx-query.html#find">
            
                    
                    find()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="rx-query.html">
            
                <a href="rx-query.html#findOne">
            
                    
                    findOne()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="rx-query.html">
            
                <a href="rx-query.html#exec">
            
                    
                    exec()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="rx-query.html">
            
                <a href="rx-query.html#observe-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="rx-query.html">
            
                <a href="rx-query.html#update">
            
                    
                    update()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="rx-query.html">
            
                <a href="rx-query.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="rx-query.html">
            
                <a href="rx-query.html#doesDocumentDataMatch">
            
                    
                    doesDocumentDataMatch()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="rx-query.html">
            
                <a href="rx-query.html#setting-a-specific-index">
            
                    
                    Specific Index
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="rx-query.html">
            
                <a href="rx-query.html#examples">
            
                    
                    Examples
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.10" data-path="rx-query.html">
            
                <a href="rx-query.html#isrxquery">
            
                    
                    isRxQuery()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="rx-storage.html">
            
                <a href="rx-storage.html">
            
                    
                    RxStorage
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="rx-storage-performance.html">
            
                <a href="rx-storage-performance.html">
            
                    
                    RxStorage Performance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="rx-storage-dexie.html">
            
                <a href="rx-storage-dexie.html">
            
                    
                    RxStorage Dexie.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="rx-storage-lokijs.html">
            
                <a href="rx-storage-lokijs.html">
            
                    
                    RxStorage LokiJS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="rx-storage-memory.html">
            
                <a href="rx-storage-memory.html">
            
                    
                    RxStorage Memory
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="rx-storage-indexeddb.html">
            
                <a href="rx-storage-indexeddb.html">
            
                    
                    RxStorage IndexedDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.6" data-path="rx-storage-sqlite.html">
            
                <a href="rx-storage-sqlite.html">
            
                    
                    RxStorage SQLite
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.7" data-path="rx-storage-foundationdb.html">
            
                <a href="rx-storage-foundationdb.html">
            
                    
                    RxStorage FoundationDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.8" data-path="rx-storage-worker.html">
            
                <a href="rx-storage-worker.html">
            
                    
                    RxStorage Worker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.9" data-path="rx-storage-shared-worker.html">
            
                <a href="rx-storage-shared-worker.html">
            
                    
                    RxStorage Shared Worker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.10" data-path="rx-storage-remote.html">
            
                <a href="rx-storage-remote.html">
            
                    
                    RxStorage Remote
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.11" data-path="rx-storage-memory-synced.html">
            
                <a href="rx-storage-memory-synced.html">
            
                    
                    RxStorage Memory Synced
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.12" data-path="rx-storage-sharding.html">
            
                <a href="rx-storage-sharding.html">
            
                    
                    RxStorage Sharding
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.13" data-path="rx-storage-localstorage-meta-optimizer.html">
            
                <a href="rx-storage-localstorage-meta-optimizer.html">
            
                    
                    RxStorage Localstorage Meta Optimizer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.14" data-path="electron.html">
            
                <a href="electron.html#rxstorage-electron-ipcrenderer--ipcmain">
            
                    
                    RxStorage Electron IpcRenderer & IpcMain
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="replication.html">
            
                <a href="replication.html">
            
                    
                    Replication
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.12.1" data-path="replication-graphql.html">
            
                <a href="replication-graphql.html">
            
                    
                    Replication GraphQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="replication-websocket.html">
            
                <a href="replication-websocket.html">
            
                    
                    Replication Websocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="replication-couchdb.html">
            
                <a href="replication-couchdb.html">
            
                    
                    Replication CouchDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="replication-p2p.html">
            
                <a href="replication-p2p.html">
            
                    
                    Replication P2P
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="replication-firestore.html">
            
                <a href="replication-firestore.html">
            
                    
                    Replication Firestore
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="rx-attachment.html">
            
                <a href="rx-attachment.html">
            
                    
                    RxAttachment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="middleware.html">
            
                <a href="middleware.html">
            
                    
                    Middleware-hooks
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="orm.html">
            
                <a href="orm.html">
            
                    
                    ORM/DRM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="population.html">
            
                <a href="population.html">
            
                    
                    Population
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="encryption.html">
            
                <a href="encryption.html">
            
                    
                    Encryption
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="key-compression.html">
            
                <a href="key-compression.html">
            
                    
                    Key Compression
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="data-migration.html">
            
                <a href="data-migration.html">
            
                    
                    DataMigration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="leader-election.html">
            
                <a href="leader-election.html">
            
                    
                    LeaderElection
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="cleanup.html">
            
                <a href="cleanup.html">
            
                    
                    Cleanup
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="backup.html">
            
                <a href="backup.html">
            
                    
                    Backup
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="query-cache.html">
            
                <a href="query-cache.html">
            
                    
                    QueryCache
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="rx-local-document.html">
            
                <a href="rx-local-document.html">
            
                    
                    LocalDocuments
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="crdt.html">
            
                <a href="crdt.html">
            
                    
                    CRDT
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="third-party-plugins.html">
            
                <a href="third-party-plugins.html">
            
                    
                    Third Party Plugins
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="plugins.html">
            
                <a href="plugins.html">
            
                    
                    Creating Plugins
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="query-optimizer.html">
            
                <a href="query-optimizer.html">
            
                    
                    Query Optimizer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="storage-migration.html">
            
                <a href="storage-migration.html">
            
                    
                    Storage Migration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="transactions-conflicts-revisions.html">
            
                <a href="transactions-conflicts-revisions.html">
            
                    
                    Transactions, Conflicts and Revisions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" >
            
                <a target="_blank" href="https://rxdb.info/premium.html">
            
                    
                    RxDB Premium
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" >
            
                <span>
            
                    
                    Tutorials
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.12.1" data-path="tutorials/typescript.html">
            
                <a href="tutorials/typescript.html">
            
                    
                    Use RxDB with Typescript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12.2" data-path="nodejs-database.html">
            
                <a href="nodejs-database.html">
            
                    
                    RxDB as Node.js Database
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.13" >
            
                <span>
            
                    
                    Opinions
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.13.1" data-path="offline-first.html">
            
                <a href="offline-first.html">
            
                    
                    Offline First
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.2" data-path="downsides-of-offline-first.html">
            
                <a href="downsides-of-offline-first.html">
            
                    
                    Downsides of Offline First
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.3" data-path="slow-indexeddb.html">
            
                <a href="slow-indexeddb.html">
            
                    
                    Slow IndexedDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.4" data-path="why-nosql.html">
            
                <a href="why-nosql.html">
            
                    
                    Why NoSQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.5" data-path="alternatives.html">
            
                <a href="alternatives.html">
            
                    
                    Alternatives
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.6" data-path="react-native-database.html">
            
                <a href="react-native-database.html">
            
                    
                    React Native Database
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.7" data-path="capacitor-database.html">
            
                <a href="capacitor-database.html">
            
                    
                    Capacitor Database
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13.8" data-path="electron-database.html">
            
                <a href="electron-database.html">
            
                    
                    Electron Database
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="questions-answers.html">
            
                <a href="questions-answers.html">
            
                    
                    Questions & Answers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="contribute.html">
            
                <a href="contribute.html">
            
                    
                    Contribute
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16" >
            
                <span>
            
                    
                    Release Notes
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.16.1" >
            
                <a target="_blank" href="https://github.com/pubkey/rxdb/blob/master/CHANGELOG.md">
            
                    
                    CHANGELOG
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.2" data-path="releases/14.0.0.html">
            
                <a href="releases/14.0.0.html">
            
                    
                    14.0.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.3" data-path="releases/13.0.0.html">
            
                <a href="releases/13.0.0.html">
            
                    
                    13.0.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.4" data-path="releases/12.0.0.html">
            
                <a href="releases/12.0.0.html">
            
                    
                    12.0.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.5" data-path="releases/11.0.0.html">
            
                <a href="releases/11.0.0.html">
            
                    
                    11.0.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.6" data-path="releases/10.0.0.html">
            
                <a href="releases/10.0.0.html">
            
                    
                    10.0.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.7" data-path="releases/9.0.0.html">
            
                <a href="releases/9.0.0.html">
            
                    
                    9.0.0
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16.8" data-path="releases/8.0.0.html">
            
                <a href="releases/8.0.0.html">
            
                    
                    8.0.0
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.17" >
            
                <span>
            
                    
                    Deprecated
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.17.1" data-path="rx-storage-pouchdb.html">
            
                <a href="rx-storage-pouchdb.html">
            
                    
                    RxStorage PouchDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.17.2" data-path="adapters.html">
            
                <a href="adapters.html">
            
                    
                    RxStorage PouchDB Adapters
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <a target="_blank" href="https://rxdb.info/imprint.html">
            
                    
                    Imprint
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Replication GraphQL</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="replication-with-graphql">Replication with GraphQL</h1>
<p>The GraphQL replication provides handlers for GraphQL to run <a href="replication.html">replication</a> with GraphQL as the transportation layer.</p>
<p><strong>NOTICE:</strong> To play around, check out the full example of the RxDB <a href="https://github.com/pubkey/rxdb/tree/master/examples/graphql" target="_blank">GraphQL replication with server and client</a></p>
<h2 id="usage">Usage</h2>
<p>Before you use the GraphQL replication, make sure you&apos;ve learned how the <a href="replication.html">RxDB replication</a> works.</p>
<h3 id="creating-a-compatible-graphql-server">Creating a compatible GraphQL Server</h3>
<p>At the server-side, there must exist an endpoint which returns newer rows when the last <code>checkpoint</code> is used as input. For example lets say you create a <code>Query</code> <code>pullHuman</code> which returns a list of document writes that happened after the given checkpoint.</p>
<p>For the push-replication, you also need a <code>Mutation</code> <code>pushHuman</code> which lets RxDB update data of documents by sending the previous document state and the new client document state.
Also for being able to stream all ongoing events, we need a <code>Subscription</code> called <code>streamHuman</code>.</p>
<pre><code class="lang-graphql">input HumanInput {
    id: ID!,
    name: String!,
    lastName: String!,
    updatedAt: Float!,
    deleted: Boolean!
}
type Human {
    id: ID!,
    name: String!,
    lastName: String!,
    updatedAt: Float!,
    deleted: Boolean!
}
input Checkpoint {
    id: String!,
    updatedAt: Float!
}
type HumanPullBulk {
    documents: [Human]!
    checkpoint: Checkpoint
}

type Query {
    pullHuman(checkpoint: Checkpoint, limit: Int!): HumanPullBulk!
}

input HumanInputPushRow {
    assumedMasterState: HeroInputPushRowT0AssumedMasterStateT0
    newDocumentState: HeroInputPushRowT0NewDocumentStateT0!
}

type Mutation {
    # Returns a list of all conflicts
    # If no document write caused a conflict, return an empty list.
    pushHuman(rows: [HumanInputPushRow!]): [Human]
}

# headers are used to authenticate the subscriptions
# over websockets.
input Headers {
    AUTH_TOKEN: String!;
}
type Subscription {
    streamHuman(headers: Headers): HumanPullBulk!
}
</code></pre>
<p>The GraphQL resolver for the <code>pullHuman</code> would then look like:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> rootValue = {
    pullHuman: args =&gt; {
        <span class="hljs-keyword">const</span> minId = args.checkpoint : args.checkpoint.id : <span class="hljs-string">&apos;&apos;</span>;
        <span class="hljs-keyword">const</span> minUpdatedAt = args.checkpoint : args.checkpoint.updatedAt : <span class="hljs-number">0</span>;

        <span class="hljs-comment">// sorted by updatedAt first and the id as second</span>
        <span class="hljs-keyword">const</span> sortedDocuments = documents.sort((a, b) =&gt; {
            <span class="hljs-keyword">if</span> (a.updatedAt &gt; b.updatedAt) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
            <span class="hljs-keyword">if</span> (a.updatedAt &lt; b.updatedAt) <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
            <span class="hljs-keyword">if</span> (a.updatedAt === b.updatedAt) {
                <span class="hljs-keyword">if</span> (a.id &gt; b.id) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
                <span class="hljs-keyword">if</span> (a.id &lt; b.id) <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
            }
        });

        <span class="hljs-comment">// only return documents newer than the input document</span>
        <span class="hljs-keyword">const</span> filterForMinUpdatedAtAndId = sortedDocuments.filter(doc =&gt; {
            <span class="hljs-keyword">if</span> (doc.updatedAt &lt; minUpdatedAt) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
            <span class="hljs-keyword">if</span> (doc.updatedAt &gt; minUpdatedAt) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
            <span class="hljs-keyword">if</span> (doc.updatedAt === minUpdatedAt) {
                <span class="hljs-comment">// if updatedAt is equal, compare by id</span>
                <span class="hljs-keyword">if</span> (doc.id &gt; minId) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
                <span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
            }
        });

        <span class="hljs-comment">// only return some documents in one batch</span>
        <span class="hljs-keyword">const</span> limitedDocs = filterForMinUpdatedAtAndId.slice(<span class="hljs-number">0</span>, args.limit);

        <span class="hljs-comment">// use the last document for the checkpoint</span>
        <span class="hljs-keyword">const</span> lastDoc = limitedDocs[limitedDocs.length - <span class="hljs-number">1</span>];
        <span class="hljs-keyword">const</span> retCheckpoint = {
            id: lastDoc.id,
            updatedAt: lastDoc.updatedAt
        }

        <span class="hljs-keyword">return</span> {
            documents: limitedDocs,
            checkpoint: retCheckpoint
        }

        <span class="hljs-keyword">return</span> limited;
    }
}
</code></pre>
<p>For examples for the other resolvers, consult the <a href="https://github.com/pubkey/rxdb/blob/master/examples/graphql/server/index.js" target="_blank">GraphQL Example Project</a>.</p>
<h3 id="rxdb-client">RxDB Client</h3>
<h4 id="pull-replication">Pull replication</h4>
<p>For the pull-replication, you first need a <code>pullQueryBuilder</code>. This is a function that gets the last replication <code>checkpoint</code> and a <code>limit</code> as input and returns an object with a GraphQL-query and its variables (or a promise that resolves to the same object). RxDB will use the query builder to construct what is later sent to the GraphQL endpoint.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> pullQueryBuilder = (checkpoint, limit) =&gt; {
    <span class="hljs-comment">/**
     * The first pull does not have a checkpoint
     * so we fill it up with defaults
     */</span>
    <span class="hljs-keyword">if</span> (!checkpoint) {
        checkpoint = {
            id: <span class="hljs-string">&apos;&apos;</span>,
            updatedAt: <span class="hljs-number">0</span>
        };
    }
    <span class="hljs-keyword">const</span> query = <span class="hljs-string">`query PullHuman($checkpoint: CheckpointInput, $limit: Int!) {
        pullHuman(checkpoint: $checkpoint, limit: $limit) {
            documents {
                id
                name
                age
                updatedAt
                deleted
            }
            checkpoint {
                id
                updatedAt
            }
        }
    }`</span>;
    <span class="hljs-keyword">return</span> {
        query,
        variables: {
            checkpoint,
            limit
        }
    };
};
</code></pre>
<p>With the queryBuilder, you can then setup the pull-replication.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { replicateGraphQL } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;rxdb/plugins/replication-graphql&apos;</span>;
<span class="hljs-keyword">const</span> replicationState = replicateGraphQL(
    {
        collection: myRxCollection,
        <span class="hljs-comment">// urls to the GraphQL endpoints</span>
        url: {
            http: <span class="hljs-string">&apos;http://example.com/graphql&apos;</span>
        },
        pull: {
            queryBuilder: pullQueryBuilder, <span class="hljs-comment">// the queryBuilder from above</span>
            modifier: doc =&gt; doc, <span class="hljs-comment">// (optional) modifies all pulled documents before they are handled by RxDB</span>
            dataPath: <span class="hljs-literal">undefined</span>, <span class="hljs-comment">// (optional) specifies the object path to access the document(s). Otherwise, the first result of the response data is used.</span>
            <span class="hljs-comment">/**
             * Amount of documents that the remote will send in one request.
             * If the response contains less then [batchSize] documents,
             * RxDB will assume there are no more changes on the backend
             * that are not replicated.
             * This value is the same as the limit in the pullHuman() schema.
             * [default=100]
             */</span>
            batchSize: <span class="hljs-number">50</span>
        },
        <span class="hljs-comment">// headers which will be used in http requests against the server.</span>
        headers: {
            Authorization: <span class="hljs-string">&apos;Bearer abcde...&apos;</span>
        },

        <span class="hljs-comment">/**
         * Options that have been inherited from the RxReplication
         */</span>
        deletedField: <span class="hljs-string">&apos;deleted&apos;</span>,
        live: <span class="hljs-literal">true</span>,
        retryTime = <span class="hljs-number">1000</span> * <span class="hljs-number">5</span>,
        waitForLeadership = <span class="hljs-literal">true</span>,
        autoStart = <span class="hljs-literal">true</span>,
    }
);
</code></pre>
<h4 id="push-replication">Push replication</h4>
<p>For the push-replication, you also need a <code>queryBuilder</code>. Here, the builder receives a changed document as input which has to be send to the server. It also returns a GraphQL-Query and its data.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> pushQueryBuilder = rows =&gt; {
    <span class="hljs-keyword">const</span> query = <span class="hljs-string">`
    mutation PushHuman($writeRows: [HumanInputPushRow!]) {
        pushHuman(writeRows: $writeRows) {
            id
            name
            age
            updatedAt
            deleted
        }
    }
    `</span>;
    <span class="hljs-keyword">const</span> variables = {
        writeRows: rows
    };
    <span class="hljs-keyword">return</span> {
        query,
        variables
    };
};
</code></pre>
<p>With the queryBuilder, you can then setup the push-replication.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> replicationState = replicateGraphQL(
    {
        collection: myRxCollection,
        <span class="hljs-comment">// urls to the GraphQL endpoints</span>
        url: {
            http: <span class="hljs-string">&apos;http://example.com/graphql&apos;</span>
        },
        push: {
            queryBuilder: pushQueryBuilder, <span class="hljs-comment">// the queryBuilder from above</span>
            <span class="hljs-comment">/**
             * batchSize (optional)
             * Amount of document that will be pushed to the server in a single request.
             */</span>
            batchSize: <span class="hljs-number">5</span>,
            <span class="hljs-comment">/**
             * modifier (optional)
             * Modifies all pushed documents before they are send to the GraphQL endpoint.
             * Returning null will skip the document.
             */</span>
            modifier: doc =&gt; doc
        },
        headers: {
            Authorization: <span class="hljs-string">&apos;Bearer abcde...&apos;</span>
        },
        pull: {
            <span class="hljs-comment">/* ... */</span>
        },
        <span class="hljs-comment">/* ... */</span>
    }
);
</code></pre>
<h4 id="pull-stream">Pull Stream</h4>
<p>To create a <strong>realtime</strong> replication, you need to create a pull stream that pulls ongoing writes from the server.
The pull stream gets the <code>headers</code> of the <code>RxReplicationState</code> as input, so that it can be authenticated on the backend.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> pullStreamQueryBuilder = (headers) =&gt; {
    <span class="hljs-keyword">const</span> query = <span class="hljs-string">`subscription onStream($headers: Headers) {
        streamHero(headers: $headers) {
            documents {
                id,
                name,
                age,
                updatedAt,
                deleted
            },
            checkpoint {
                id
                updatedAt
            }
        }
    }`</span>;
    <span class="hljs-keyword">return</span> {
        query,
        variables: {
            headers
        }
    };
};
</code></pre>
<p>With the <code>pullStreamQueryBuilder</code> you can then start a realtime replication.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> replicationState = replicateGraphQL(
    {
        collection: myRxCollection,
        <span class="hljs-comment">// urls to the GraphQL endpoints</span>
        url: {
            http: <span class="hljs-string">&apos;http://example.com/graphql&apos;</span>,
            ws: <span class="hljs-string">&apos;ws://example.com/subscriptions&apos;</span> <span class="hljs-comment">// &lt;- The websocket has to use a different url.</span>
        },
        push: {
            batchSize: <span class="hljs-number">100</span>,
            queryBuilder: pushQueryBuilder
        },
        headers: {
            Authorization: <span class="hljs-string">&apos;Bearer abcde...&apos;</span>
        },
        pull: {
            batchSize: <span class="hljs-number">100</span>,
            queryBuilder: pullQueryBuilder,
            streamQueryBuilder: pullStreamQueryBuilder,
            includeWsHeaders: <span class="hljs-literal">false</span>, <span class="hljs-comment">// Includes headers as connection parameter to Websocket.</span>
        },
        deletedField: <span class="hljs-string">&apos;deleted&apos;</span>
    }
);
</code></pre>
<p><strong>NOTICE</strong>: If it is not possible to create a websocket server on your backend, you can use any other method of pull out the ongoing events from the backend and then you can send them into <code>RxReplicationState.emitEvent()</code>.</p>
<h3 id="transforming-null-to-undefined-in-optional-fields">Transforming null to undefined in optional fields</h3>
<p>GraphQL fills up non-existent optional values with <code>null</code> while RxDB required them to be <code>undefined</code>.
Therefore, if your schema contains optional properties, you have to transform the pulled data to switch out <code>null</code> to <code>undefined</code></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> replicationState: RxGraphQLReplicationState&lt;RxDocType&gt; = replicateGraphQL(
    {
        collection: myRxCollection,
        url: {<span class="hljs-comment">/* ... */</span>},
        headers: {<span class="hljs-comment">/* ... */</span>},
        push: {<span class="hljs-comment">/* ... */</span>},
        pull: {
            queryBuilder: pullQueryBuilder,
            modifier: (doc =&gt; {
                <span class="hljs-comment">//Wwe have to remove optional non-existent field values</span>
                <span class="hljs-comment">// they are set as null by GraphQL but should be undefined</span>
                <span class="hljs-built_in">Object</span>.entries(doc).forEach(([k, v]) =&gt; {
                    <span class="hljs-keyword">if</span> (v === <span class="hljs-literal">null</span>) {
                        <span class="hljs-keyword">delete</span> doc[k];
                    }
                });
                <span class="hljs-keyword">return</span> doc;
            })
        },
        <span class="hljs-comment">/* ... */</span>
    }
);
</code></pre>
<h3 id="pullresponsemodifier">pull.responseModifier</h3>
<p>With the <code>pull.responseModifier</code> you can modify the whole response from the GraphQL endpoint <strong>before</strong> it is processed by RxDB.
For example if your endpoint is not capable of returning a valid checkpoint, but instead only returns the plain document array, you can use the <code>responseModifier</code> to aggregate the checkpoint from the returned documents.</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> {

} from <span class="hljs-string">&apos;rxdb&apos;</span>;
<span class="hljs-keyword">const</span> replicationState: RxGraphQLReplicationState&lt;RxDocType&gt; = replicateGraphQL(
    {
        collection: myRxCollection,
        url: {<span class="hljs-comment">/* ... */</span>},
        headers: {<span class="hljs-comment">/* ... */</span>},
        push: {<span class="hljs-comment">/* ... */</span>},
        pull: {
            responseModifier: async <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">
                plainResponse, <span class="hljs-comment">// the exact response that was returned from the server</span>
                origin, <span class="hljs-comment">// either &apos;handler&apos; if plainResponse came from the pull.handler, or &apos;stream&apos; if it came from the pull.stream</span>
                requestCheckpoint <span class="hljs-comment">// if origin===&apos;handler&apos;, the requestCheckpoint contains the checkpoint that was send to the backend</span>
            </span>) </span>{
                <span class="hljs-comment">/**
                 * In this example we aggregate the checkpoint from the documents array
                 * that was returned from the graphql endpoint.
                 */</span>
                <span class="hljs-keyword">const</span> docs = plainResponse;
                <span class="hljs-keyword">return</span> {
                    documents: docs,
                    checkpoint: docs.length === <span class="hljs-number">0</span> ? requestCheckpoint : {
                        name: lastOfArray(docs).name,
                        updatedAt: lastOfArray(docs).updatedAt
                    }
                };
            }
        },
        <span class="hljs-comment">/* ... */</span>
    }
);
</code></pre>
<h3 id="pushresponsemodifier">push.responseModifier</h3>
<p>It&apos;s also possible to modify the response of a push mutation. For example if your server returns more than the just conflicting docs:</p>
<pre><code class="lang-graphql">type PushResponse {
    conflicts: [Human]
    conflictMessages: [ReplicationConflictMessage]
}

type Mutation {
    # Returns a PushResponse type that contains the conflicts along with other information
    pushHuman(rows: [HumanInputPushRow!]): PushResponse!
}
</code></pre>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> {} from <span class="hljs-string">&quot;rxdb&quot;</span>;
<span class="hljs-keyword">const</span> replicationState: RxGraphQLReplicationState&lt;RxDocType&gt; = replicateGraphQL(
    {
        collection: myRxCollection,
        url: {<span class="hljs-comment">/* ... */</span>},
        headers: {<span class="hljs-comment">/* ... */</span>},
        push: {
            responseModifier: async <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">plainResponse</span>) </span>{
                <span class="hljs-comment">/**
                 * In this example we aggregate the conflicting documents from a response object
                 */</span>
                <span class="hljs-keyword">return</span> plainResponse.conflicts;
            },
        },
        pull: {<span class="hljs-comment">/* ... */</span>},
        <span class="hljs-comment">/* ... */</span>
    }
);
</code></pre>
<h4 id="helper-functions">Helper Functions</h4>
<p>RxDB provides the helper functions <code>graphQLSchemaFromRxSchema()</code>, <code>pullQueryBuilderFromRxSchema()</code>, <code>pullStreamBuilderFromRxSchema()</code> and <code>pushQueryBuilderFromRxSchema()</code> that can be used to generate handlers and schemas from the <code>RxJsonSchema</code>. To learn how to use them, please inspect the <a href="https://github.com/pubkey/rxdb/tree/master/examples/graphql" target="_blank">GraphQL Example</a>.</p>
<h3 id="rxgraphqlreplicationstate">RxGraphQLReplicationState</h3>
<p>When you call <code>myCollection.syncGraphQL()</code> it returns a <code>RxGraphQLReplicationState</code> which can be used to subscribe to events, for debugging or other functions. It extends the <a href="replication.html">RxReplicationState</a> with some GraphQL specific methods.</p>
<h4 id="setheaders">.setHeaders()</h4>
<p>Changes the headers for the replication after it has been set up.</p>
<pre><code class="lang-js">replicationState.setHeaders({
    Authorization: <span class="hljs-string">`...`</span>
});
</code></pre>
<h4 id="sending-cookies">Sending Cookies</h4>
<p>The underlying fetch framework uses a <code>same-origin</code> policy for credentials per default. That means, cookies and session data is only shared if you backend and frontend run on the same domain and port. Pass the credential parameter to <code>include</code> cookies in requests to servers from different origins via:</p>
<pre><code class="lang-js">replicationState.setCredentials(<span class="hljs-string">&apos;include&apos;</span>);
</code></pre>
<p>or directly pass it in the the <code>syncGraphQL</code>:</p>
<pre><code class="lang-js">replicateGraphQL(
    {
        collection: myRxCollection,
        <span class="hljs-comment">/* ... */</span>
        credentials: <span class="hljs-string">&apos;include&apos;</span>,
        <span class="hljs-comment">/* ... */</span>
    }
);
</code></pre>
<p>See <a href="https://fetch.spec.whatwg.org/#concept-request-credentials-mode" target="_blank">the fetch spec</a> for more information about available options.</p>
<p><strong>NOTICE:</strong> To play around, check out the full example of the RxDB <a href="https://github.com/pubkey/rxdb/tree/master/examples/graphql" target="_blank">GraphQL replication with server and client</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="replication.html" class="navigation navigation-prev " aria-label="Previous page: Replication">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="replication-websocket.html" class="navigation navigation-next " aria-label="Next page: Replication Websocket">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Replication GraphQL","level":"1.12.1","depth":2,"next":{"title":"Replication Websocket","level":"1.12.2","depth":2,"path":"replication-websocket.md","ref":"./replication-websocket.md","articles":[]},"previous":{"title":"Replication","level":"1.12","depth":1,"path":"replication.md","ref":"./replication.md","articles":[{"title":"Replication GraphQL","level":"1.12.1","depth":2,"path":"replication-graphql.md","ref":"./replication-graphql.md","articles":[]},{"title":"Replication Websocket","level":"1.12.2","depth":2,"path":"replication-websocket.md","ref":"./replication-websocket.md","articles":[]},{"title":"Replication CouchDB","level":"1.12.3","depth":2,"path":"replication-couchdb.md","ref":"./replication-couchdb.md","articles":[]},{"title":"Replication P2P","level":"1.12.4","depth":2,"path":"replication-p2p.md","ref":"./replication-p2p.md","articles":[]},{"title":"Replication Firestore","level":"1.12.5","depth":2,"path":"replication-firestore.md","ref":"./replication-firestore.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["edit-link","github","custom-favicon","-sharing","expandable-chapters","scripts"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/pubkey/rxdb"},"scripts":{"files":["./analytics.js","./next-button.js"]},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":"./files/logo/logo.svg","custom-favicon":{},"edit-link":{"label":"","base":"https://github.com/pubkey/rxdb/tree/master/docs-src"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"RxDB - JavaScript Database","links":{"sidebar":{"RxDB":"/","Follow @twitter ":"https://twitter.com/intent/user?screen_name=rxdbjs","Chat @discord ":"https://discord.gg/tqt9ZttJfD","Star @github ":"https://github.com/pubkey/rxdb","Donate @github ":"https://github.com/sponsors/pubkey"}},"gitbook":"3.2.3"},"file":{"path":"replication-graphql.md","mtime":"","type":"markdown"},"gitbook":{"version":"3.2.3","time":""},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-scripts/28fd35f20e40e37a9994a4812fc3c7cc-analytics.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-scripts/9bc541ba5d318730a5b44c9770ae18fc-next-button.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

